package com.leaves.javamall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.leaves.javamall.product.dao.BrandDao;
import com.leaves.javamall.product.dao.CategoryDao;
import com.leaves.javamall.product.entity.BrandEntity;
import com.leaves.javamall.product.entity.CategoryEntity;
import com.leaves.javamall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leaves.common.utils.PageUtils;
import com.leaves.common.utils.Query;

import com.leaves.javamall.product.dao.CategoryBrandRelationDao;
import com.leaves.javamall.product.entity.CategoryBrandRelationEntity;
import com.leaves.javamall.product.service.CategoryBrandRelationService;

import javax.validation.constraints.NotBlank;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    BrandDao brandDao;

    @Autowired
    CategoryDao categoryDao;

    @Autowired
    CategoryBrandRelationDao relationDao;

    @Autowired
    BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetails(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();

        BrandEntity brandEntity = brandDao.selectById(brandId);
        String brandName = brandEntity.getName();

        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        String catelogeName = categoryEntity.getName();

        categoryBrandRelation.setBrandName(brandName);
        categoryBrandRelation.setCatelogName(catelogeName);

        this.save(categoryBrandRelation);


    }

    @Override
    public void updateBrandDetails(Long brandId, String name) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setBrandName(name);
        entity.setBrandId(brandId);
        this.update(entity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    }

    @Override
    public void updateCategoryDetails(String name, Long catId) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setCatelogName(name);
        entity.setCatelogId(catId);
        System.out.println("传入关系表更新的数据"+name+catId);
        this.update(entity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));
        System.out.println("更新关系表中的数据"+entity.getCatelogName()+catId+ entity.getCatelogName());
    }

    @Override
    //查询指定分类的品牌信息
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        //在品牌和分类的关联表中找到关联
        List<CategoryBrandRelationEntity> entityList = relationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));

        List<BrandEntity> brandCollect = entityList.stream().map(item -> {
            Long id = item.getBrandId();
            BrandEntity brand = brandService.getById(id);
            return brand;
        }).collect(Collectors.toList());

        return brandCollect;
    }

}